/**
* @Company 青鸟软通   
* @Title: URLUtil.java 
* @Package com.haier.isales.common 
* @author Liu Wenjie   
* @date 2014-11-13 下午8:45:31 
* @version V1.0   
*/ 
package com.jbinfo.qingcaowan.common;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/** 
 * @ClassName: URLUtil 
 * @Description: URLUtil的几个验证方法
 *  
 */
public class URLUtil {
	/** 
	* @Description: 判断是否是已授权的url
	* @author Liu Wenjie   
	* @date 2014-11-13 下午2:42:51 
	* @param url  
	*/ 
	public static boolean authenticatorUrl(String url,String resource){
		if(url == null){
			return false;
		}
		String regStr = getRegStr(resource);
		
		Pattern p = Pattern.compile(regStr);
		Matcher matcher = p.matcher(url);
		return matcher.find();
	}
	
	
	/** 
	* @Description: 获取资源对应的匹配字符
	* @author Liu Wenjie   
	* @date 2014-11-13 下午5:42:48 
	* @param url
	* @return  
	*/
	public static String getRegStr(String resource){
		Pattern p = Pattern.compile("\\{\\w+\\}");
		Matcher matcher = p.matcher(resource);
		StringBuffer sb = new StringBuffer();
		while(matcher.find()){
	        matcher.appendReplacement(sb, ".+");
	    }
		matcher.appendTail(sb);
		String regx = sb.toString();
		if(regx.endsWith("/")){
			regx.substring(0,regx.length() -1);
		}
		return "^" + regx + "/*$";
	}
}
